package cn.edu.scau.cmi.tancuier.client;

import cn.edu.scau.cmi.tancuier.singleton.Chairman;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class SingletonClient extends Application{
	
    @Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("Singleton");
		
		FlowPane flowPane = new FlowPane();
		flowPane.setPadding(new Insets(10, 20, 10, 20));
		flowPane.setHgap(5);
		flowPane.setVgap(5);
		
		Label name = new Label("Name: ");
		flowPane.getChildren().add(name);
		
		TextField input = new TextField("");
		flowPane.getChildren().add(input);
		
		Button button = new Button("Create");
		button.setOnAction(e->{
			Label label = null;
			Chairman chairman = Chairman.getInstance(input.getText());
			if(chairman == null) {
				label = new Label("You want to create "+input.getText() + ", but this kind of object has been created. Please stop creating. ");
			}
			else {
				label = new Label("The chairman that you have just created: " + chairman.getName() + ", object: " + chairman);
			}
			flowPane.getChildren().add(label);
		});
		flowPane.getChildren().add(button);
		
		Scene scene = new Scene(flowPane, 600, 300);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		Application.launch(args);
	}
	
}
